package com.anosine.gpsl.android.ui.activity;

import java.io.UnsupportedEncodingException;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.anosine.gpsl.android.GPSLApp;
import com.anosine.gpsl.android.R;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.presenter.BasePresenterActivity;
import com.anosine.gpsl.android.presenter.LoginPresenter;
import com.anosine.gpsl.android.presenter.ValidatePresenter;
import com.anosine.gpsl.android.ui.LoginView;
import com.anosine.gpsl.android.ui.ValidateView;
import com.anosine.gpsl.android.util.ActivityFrgManager;
import com.anosine.gpsl.android.util.DeviceUtil;
import com.anosine.gpsl.android.util.StringUtil;
import com.anosine.gpsl.android.util.ToastHelper;
import com.anosine.gpsl.android.util.UserInfoProxy;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * @Title: LoginActivity
 * @Package com.anosine.gpsl.android.ui.activity
 * @Description: 登录界面
 * @author: sunny
 * @date: 2015-11-18
 * @version V1.0
 */
public class LoginActivity extends BasePresenterActivity implements LoginView,
		ValidateView {
	@ViewInject(R.id.et_user)
	EditText et_user;
	@ViewInject(R.id.et_pwd)
	EditText et_pwd;
	@ViewInject(R.id.et_login_realname)
	EditText et_realname;
	@ViewInject(R.id.et_login_idno)
	EditText et_idno;

	/**
	 * @Title: LoginOnClick(View v)
	 * @Description: 登录按钮的点击事件，点击连接服务器，实现登录功能
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.login)
	private void LoginOnClick(View v) {
		// 启动MVP
		initActivityOfLogin();
	}

	/**
	 * @Title: ForgetPassOnClick(View v)
	 * @Description: 忘记密码按钮的点击事件
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.btn_login_forget_pass)
	private void ForgetPassOnClick(View v) {
		Intent intent = new Intent();
		// 设置传递方向
		intent.setClass(getApplication(), ValidateOrForgetPwdActivity.class);
		// 绑定数据
		intent.putExtra("status", "forget");
		startActivity(intent);
	}

	/**
	 * @Title: ForgetPassOnClick(View v)
	 * @Description: 注册按钮的点击事件，跳转到注册页面
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.register)
	private void RegisterOnClick(View v) {
		Intent Register = new Intent(LoginActivity.this, RegisterActivity.class);
		startActivity(Register);
	}

	/**
	 * @Title: onCreate(Bundle savedInstanceState)
	 * @Description: 初始化登录页面
	 * @param: savedInstanceState
	 * @return: 无
	 * @throws
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		ViewUtils.inject(this);
	}

	/**
	 * @Title: setLogin(Login login)
	 * @Description: 获取解析完后的登录信息
	 * @param: login
	 * @return: 无
	 * @throws
	 */
	@Override
	public void setLogin(Login t) {
		// 返回的信息

		if (t != null && "msg_login_suucess".equals(t.message)) {
			ActivityFrgManager.getInstance().finishActivities();
			t.isLoginAuto = true;
			t.setUserName(et_user.getText().toString());
			t.setPassWord(et_pwd.getText().toString());
			try {
				if (UserInfoProxy.getInstance().getLoginInfo().SaveOid != null) {
					t.SaveOid = UserInfoProxy.getInstance().getLoginInfo().SaveOid;
				}
			} catch (Exception e) {

			}

			UserInfoProxy.getInstance().setLoginUserInfo(t);
			startActivity(new Intent(getApplication(), GPSLMainActivity.class));
			finish();
		} else {
			if (t != null && "msg_IMEI_flase".equals(t.message)) {
				ToastHelper.toast("您本次登录与上次使用的手机不一致，请输入验证信息。");
				et_realname.setVisibility(View.VISIBLE);
				et_idno.setVisibility(View.VISIBLE);
			} else {

				if ("msg_username_flase".equals(t.message)) {
					ToastHelper.toast("你输入的用户名错误!");
				} else if ("msg_password_flase".equals(t.message)) {
					ToastHelper.toast("你输入的密码错误!");
				} else if ("msg_fail".equals(t.message)) {
					ToastHelper.toast("你输入的参数错误!");
				} else if ("msg_login_flase".equals(t.message)) {
					ToastHelper.toast("登录失败!");
				}
			}
		}
	}

	/**
	 * @Title: initActivityOfLogin()
	 * @Description: 获取登录信息
	 * @param: 无
	 * @return: 无
	 * @throws
	 */
	private void initActivityOfLogin() {
		String name = et_user.getText().toString();
		String password = et_pwd.getText().toString();
		String realname = et_realname.getText().toString();
		String idno = et_idno.getText().toString();
		if (StringUtil.isEmptyOrNull(name)) {
			ToastHelper.toast("请输入用户名");
			return;
		}
		if (StringUtil.isEmptyOrNull(password)) {
			ToastHelper.toast("请输入密码");
			return;
		}
		if (et_realname.getVisibility() == View.VISIBLE) {
			if (StringUtil.isEmptyOrNull(realname)) {
				ToastHelper.toast("请输入真实姓名");
				return;
			}
			if (StringUtil.isEmptyOrNull(idno)) {
				ToastHelper.toast("请输入身份证后8位");
				return;
			}
		}
		if (StringUtil.isNotEmpty(realname)) {
			initActivityOfValidate(name, password, realname, idno);
			return;
		}
		// 将合成的URL传给服务器
		LoginPresenter presenter = new LoginPresenter(this);
		try {
			presenter.loadLogin(name, password, DeviceUtil.getDeviceImei(),
					GPSLApp.getchannelId(), DeviceUtil.getDeviceImsi(),
					Build.BRAND, "1", GPSLApp.getuserId(), Build.MODEL,
					DeviceUtil.getVersionName(this));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void initActivityOfValidate(String name, String password,
			String realname, String idno) {
		ValidatePresenter presenter = new ValidatePresenter(this);
		presenter.loadValidate(name, DeviceUtil.getDeviceImei(), et_realname.getText().toString(), GPSLApp.getchannelId(), et_idno
				.getText().toString(), DeviceUtil.getDeviceImsi(), Build.BRAND,
				"1", GPSLApp.getuserId(), Build.MODEL);
	}

	@Override
	public void setValidate(Login t) {
		if (t != null && "msg_checklogin_suucess".equals(t.message)) {
			ActivityFrgManager.getInstance().finishActivities();
			t.isLoginAuto = true;
			t.setUserName(et_user.getText().toString());
			t.setPassWord(et_pwd.getText().toString());
			try {
				if (UserInfoProxy.getInstance().getLoginInfo().SaveOid != null) {
					t.SaveOid = UserInfoProxy.getInstance().getLoginInfo().SaveOid;
				}
			} catch (Exception e) {

			}
			UserInfoProxy.getInstance().setLoginUserInfo(t);
			startActivity(new Intent(getApplication(), GPSLMainActivity.class));
			finish();
		} else {
			if ("msg_fail".equals(t.message)) {
				ToastHelper.toast("你输入的参数错误!");
			} else if ("msg_checklogin_false".equals(t.message)) {
				ToastHelper.toast("验证登录失败!");
			}
		}
	}

}
